(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.workflow.wfSetup.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'wfSetupService' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, wfSetupService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("workflowList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			wfSetupService.getItemTypeOptions().then(function(response) {
				response.hasEmptyOption = true;
				ctrl.itemKeyOptions = response;
			});
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.search();
		};

		ctrl.process = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.process(ctrl.selectedRows[0]).then(function(response) {
				var routerInfo = response.data || {}, state = routerInfo.processState;
				delete routerInfo.processState;
				$scope.go(state, routerInfo);
			}, processFailed);
		};
		function processFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		ctrl.claim = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.claim(ctrl.selectedRows[0]).then(claimSuccessful, claimFailed);
		};
		
		function claimSuccessful(response) {
			ctrl.search();
		}

		function claimFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		ctrl.unclaim = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			authService.unclaim(ctrl.selectedRows[0]).then(unclaimSuccessful, unclaimFailed);
		};
		
		function unclaimSuccessful(response) {
			ctrl.search();
		}

		function unclaimFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);